package fuzion24.device.vulnerability.broadcastreceiver;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.nowsecure.android.vts.R;

import java.util.ArrayList;
import java.util.List;

import fuzion24.device.vulnerability.test.ui.MainActivity;
import fuzion24.device.vulnerability.util.SharedPreferencesUtils;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityOrganizer;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityTest;

/**
 * Handles the application update/installation to show a notification to the user if there are
 * new vulnerabilities scans available.
 */

public class ApplicationUpdateBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "VULN_TEST_BROAD_UPDATE";

    private static final int NOTIFICATION_ID = 1;

    private static final boolean mTestWasChanged = false;

    @Override
    public void onReceive(final Context context, final Intent intent) {
        List<String> currentTestsAvailable = new ArrayList<>();

        for (VulnerabilityTest test : VulnerabilityOrganizer.getTests(context)) {
            currentTestsAvailable.add(test.getCVEorID());
        }

        List<String> testsAvailableOnPreviousBuild = SharedPreferencesUtils.getTheListOfScansAvailable(context);

        // First run, save the current list of scans.
        if (testsAvailableOnPreviousBuild == null) {
            Log.d(TAG, "This is the first time this detection is running.");
            SharedPreferencesUtils.setTheListOfScansAvailable(context, currentTestsAvailable);

            return;
        }

        // Update application with the tests available.
        SharedPreferencesUtils.setTheListOfScansAvailable(context, currentTestsAvailable);

        if(mTestWasChanged){
            buildNotification(context, false);
            Log.d(TAG, "An existing test was updated");
        }

        for (String testName : currentTestsAvailable) {
            if (!testsAvailableOnPreviousBuild.contains(testName)) {

                // This test is new, show a notification to the user.
                buildNotification(context, true);
                Log.d(TAG, String.format("New test available:, %s", testName));

                return;
            }
        }

        Log.d(TAG, "Without new tests detected.");
    }

    private void buildNotification(Context context, boolean forNewTest) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notification)
            .setAutoCancel(true)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentText(forNewTest ? context.getString(R.string.notification_new_tests) :
                                         context.getString(R.string.existing_test_updated));

        Intent notificationIntent = new Intent(context, MainActivity.class);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        // Add the notification.
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(NOTIFICATION_ID, builder.build());
    }

}
